package com.yicong.ants.view.video;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.core.UiHelper;
import com.cchao.simplelib.ui.adapter.DataBindQuickAdapter;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.coin.TransInfo;
import com.yicong.ants.bean.video.GiftInfoBean;
import com.yicong.ants.bean.video.GiftListBean;
import com.yicong.ants.databinding.DialogVideoGiftListBinding;
import com.yicong.ants.manager.BusinessHelper;
import com.yicong.ants.manager.UserManager;
import com.yicong.ants.manager.business.Dialogs;
import com.yicong.ants.manager.http.RetrofitHelper;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;

/**
 * @author glj
 * @version 2021/3/6 0025
 */
public class VideoGiftListDialog implements View.OnClickListener {
    Context mContext;
    Dialog mDialog;
    DataBindQuickAdapter<GiftListBean> mGiftAdapter;
    DialogVideoGiftListBinding mBinding;

    TransInfo transInfo;
    CompositeDisposable mDisposable;
    String mVid;

    public VideoGiftListDialog(Context context, String vid, CompositeDisposable disposable) {
        mContext = context;
        this.mDisposable = disposable;
        this.mVid = vid;
        mDialog = new Dialog(mContext, R.style.VideoDialog);
        mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.dialog_video_gift_list, null, false);
        mDialog.setContentView(mBinding.getRoot());
        mBinding.setClick(this);
        initDialog();
        initVideoGiftAdapter();
        BusinessHelper.loadVideoGift(mDisposable, bool -> {
            if (bool) {
                mGiftAdapter.setNewData(BusinessHelper.mGiftList);
            } else {
                UiHelper.showToast("加载礼物异常，请稍候再试");
                mDialog.dismiss();
            }
        });
    }

    private void initDialog() {
        Window dialogWindow = mDialog.getWindow();
        WindowManager.LayoutParams attributes = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.BOTTOM);
        attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
        attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
        dialogWindow.setAttributes(attributes);
    }

    private void initVideoGiftAdapter() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, 2);
        gridLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mBinding.rv.setLayoutManager(gridLayoutManager);
        mGiftAdapter = new DataBindQuickAdapter<GiftListBean>(R.layout.item_gift) {
            @Override
            protected void convert(DataBindViewHolder helper, GiftListBean item) {
                helper.getBinding().setVariable(BR.bean, item);
                helper.getBinding().setVariable(BR.click, (View.OnClickListener) view -> {
                    switch (view.getId()) {
                        case R.id.mRoot:
                            getGiveInfo(item);
                            break;
                    }
                });
            }
        };
        mBinding.rv.setAdapter(mGiftAdapter);
    }

    //获取礼物信息
    private void getGiveInfo(GiftListBean item) {
        Map<String, String> map2 = new HashMap<>();
        map2.put("gift_id", item.getGift_id());
        mDisposable.add(RetrofitHelper.getApis().giftInfo(map2)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                if (respBean.isCodeFail()) {
                    UiHelper.showToast(respBean.getMsg());
                    return;
                }

                showGiveGift(item, respBean.getData());
            }, RxHelper.getErrorConsumer()));
    }

    private void showGiveGift(GiftListBean item, GiftInfoBean respBean) {
        Dialogs.giveGift(mContext, respBean, item, (amount, payPwd) -> {
            UiHelper.showConfirmActionDialog(mContext, "您确定赠送礼物吗？", (dialogInterface, i) -> {
                Map<String, String> map = new HashMap<>();
                map.put("vid", mVid);
                map.put("pwd", payPwd);
                map.put("gift_id", item.getGift_id());
                map.put("qty", amount);

                mDisposable.add(RetrofitHelper.getApis()
                    .giveGift(map)
                    .compose(RxHelper.toMain())
                    .subscribe(respBeanGiveGift -> {
                        mDialog.dismiss();
                        if (respBeanGiveGift.isCodeFail()) {
                            UiHelper.showToast(respBeanGiveGift.getMsg());
                            return;
                        }
                        UiHelper.showToast("赠送成功");
                    }, RxHelper.getErrorConsumer()));
            });
        });
    }

    public void show() {
        mDialog.show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.cancel:
                mDialog.dismiss();
                break;

            case R.id.number:
//                showGiftNumber();
                break;

            case R.id.give:

                break;
        }
    }

    private void setGiftNumber(String number) {
        mBinding.number.setText(number);
        mDialog.dismiss();
    }
}
